﻿using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Spring.Context;
using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Huirui.Cavan.Application.MainBoundedContext.Setting
{
    public class UserSettings : IUserSettings
    {
        IApplicationContext ctx = null;

        private IList<SystemConfig> systemConfigs = null;
        private Currency _defaultCurrency = null;

        public UserSettings()
        {
            ctx = ContextRegistry.GetContext();
            ISystemConfigAppService systemConfigAppService = ctx.GetObject(SystemModuleConst.SYSTEMCONFIGAPPSERVICE) as ISystemConfigAppService;
            ICurrencyAppService currencyAppService = ctx.GetObject(SystemModuleConst.CURRENCYAPPSERVICE) as ICurrencyAppService;
            _defaultCurrency = currencyAppService.GetSingleBySearch(new CurrencySearch() { IsLocal = true });
            systemConfigs = systemConfigAppService.GetBySearch(new SystemConfigSearch()).ToList();
        }

        public int Precision 
        { 
            get
            {
                return int.Parse(systemConfigs.FirstOrDefault(sc => sc.Item == "数量").Value);
            }
        }

        public int CurrencyPrecision
        {
            get
            {
                return int.Parse(systemConfigs.FirstOrDefault(sc => sc.Item == "汇率").Value);
            }
        }

        public bool PurchaseOrderShouldBeChecked
        {
            get
            {
                if (systemConfigs.FirstOrDefault(sc => sc.Item == "采购订单审核控制").Value == "1")
                {
                    return true;
                }
                return false;
            }
        }

        public bool SalesOrderShouldBeChecked
        {
            get
            {
                if (systemConfigs.FirstOrDefault(sc => sc.Item == "销售订单审核控制").Value == "1")
                {
                    return true;
                }
                return false;
            }
        }

        public Currency DefaultCurrency
        {
            get
            {
                return _defaultCurrency;
            }
        }
    }
}
